﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchFilteredCustomers
    {
        partial void SearchFilteredCustomers_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {

            if (this.Application.User.HasPermission(Permissions.CanEditCustomer) || this.Application.User.HasPermission(Permissions.CanSearchCustomer))//admin or user with search acceess
            {
            }
            //else if (this.Application.User.HasPermission(Permissions.CanSearchCustomer + Permissions.CanManageMyCustomer))
            //{
            //    string userName = this.Application.User.FullName;
            //    //sales person
            //    foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
            //    {
            //        if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
            //        {
            //            this.SalesPersonId = item.Id;
            //            break;
            //        }
            //    }

            //    ////customer
            //    //foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
            //    //{
            //    //    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
            //    //    {
            //    //        this.CustomerId = item.Id;
            //    //        break;
            //    //    }
            //    //}
            //}
            else if (this.CustomerId == null)
            {
                this.CustomerId = -1;
            }
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }

        partial void SearchFilteredCustomers_Activated()
        {
            // Write your code here.

        }

        partial void Search_Execute()
        {
            FilteredCustomers.Load();
        }

        partial void ClearFilter_Execute()
        {
            this.SelectedCustomerName = null;

            if (!this.FindControl("SelectedCustomerType").IsReadOnly)
            {
                this.SelectedCustomerType = null;
            }

            if (!this.FindControl("SelectedCustomerCode").IsReadOnly)
            {
                this.SelectedCustomerCode = null;
            }

            if (!this.FindControl("SelectedCustomerCodeTax").IsReadOnly)
            {
                this.SelectedCustomerCodeTax = null;
            }

            if (!this.FindControl("SelectedCustomerVatNo").IsReadOnly)
            {
                this.SelectedCustomerVatNo = null;
            }
        }
    }
}
